package com.example.administrator.yunniuusedcar.fragment;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.example.administrator.common.base.BaseFragment;
import com.example.administrator.common.base.MyApp;
import com.example.administrator.yunniuusedcar.R;
import com.example.administrator.yunniuusedcar.activity.ChooseCarModelsActivity;
import com.example.administrator.yunniuusedcar.activity.DetailsCarActivity;
import com.example.administrator.yunniuusedcar.adapter.CarBoradAdapter;
import com.example.administrator.yunniuusedcar.adapter.CarLevelAdapter;
import com.example.administrator.yunniuusedcar.adapter.CarPriceAdapter;
import com.example.administrator.yunniuusedcar.adapter.CarTypeAdapter;
import com.example.administrator.yunniuusedcar.bean.Brand;
import com.example.administrator.yunniuusedcar.bean.SelectCarType;
import com.example.administrator.yunniuusedcar.model.mode_benn.IHttpService;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/7.
 *  “选车” 多选
 */

public class SXScreenFragment extends BaseFragment {

    private GridView typeGridView;
    private GridView priceGridView;
    private GridView boardGridView;
    private GridView levelGridView;

    private TextView type_select_tv;
    private TextView price_select_tv;
    private TextView brand_select_tv;
    private TextView level_select_tv;

    //选中的id
    private int type_id = 1;
    private int brand_id = 1;
    private int level_id = 1;
    private int price_id = 1;

    private CarTypeAdapter carTypeAdapter = null;
    private CarPriceAdapter carPriceAdapter =null;
    private CarBoradAdapter carBoradAdapter =null;
    private CarLevelAdapter carLevelAdapter =null;

    private TextView resetting;
    private TextView sure;

    SelectCarType selectCarBean;

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_choosecar03;
    }

    @Override
    public void initView() {
        typeGridView =(GridView)findView(R.id.type_gridview);
        priceGridView =(GridView)findView(R.id.price_gridview);
        boardGridView =(GridView)findView(R.id.brand_gridview);
        levelGridView =(GridView)findView(R.id.level_gridview);

        type_select_tv = (TextView)findView(R.id.type_select_tv);
        price_select_tv = (TextView)findView(R.id.price_select_tv);
        brand_select_tv = (TextView)findView(R.id.brand_select_tv);;
        level_select_tv= (TextView)findView(R.id.level_select_tv);;

        resetting = (TextView) findView(R.id.resetting);
        sure = (TextView)findView(R.id.sure);
    }

    @Override
    public void initListener() {





        resetting.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        resetting.setBackgroundResource(R.drawable.beijing_gridview_selectcar_true);
                        break;

                    case MotionEvent.ACTION_UP:
                        resetting.setBackgroundResource(R.drawable.beijing_gridview_selectcar_flase);
                        break;
                }

                return false;
            }
        });


        sure.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        sure.setBackgroundResource(R.drawable.beijing_gridview_selectcar_true);
                        break;

                    case MotionEvent.ACTION_UP:
                        sure.setBackgroundResource(R.drawable.beijing_gridview_selectcar_flase);
                        break;
                }

                return false;
            }
        });


        resetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                type_id = selectCarBean.getData().getType().get(0).getType_id();
                price_id = selectCarBean.getData().getPrice().get(0).getPrice_id();
                brand_id = selectCarBean.getData().getBrand().get(0).getBrand_id();
                level_id = selectCarBean.getData().getLevel().get(0).getLevel_id();

                type_select_tv.setText(selectCarBean.getData().getType().get(0).getName());
                price_select_tv.setText(selectCarBean.getData().getPrice().get(0).getRange());
                brand_select_tv.setText(selectCarBean.getData().getBrand().get(0).getName());
                level_select_tv.setText(selectCarBean.getData().getLevel().get(0).getName());

                CarBoradAdapter.POSITION = 0;
                CarLevelAdapter.POSITION = 0;
                CarPriceAdapter.POSITION = 0;
                CarTypeAdapter.POSITION = 0;

                carBoradAdapter.notifyDataSetChanged();
                carTypeAdapter.notifyDataSetChanged();
                carLevelAdapter.notifyDataSetChanged();
                carPriceAdapter.notifyDataSetChanged();

            }
        });


        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MyApp.type_id = type_id;
                MyApp.brand_id = brand_id;
                MyApp.level_id = level_id;
                MyApp.price_id =price_id;


//                startActivity(new Intent(getContext(),DetailsCarActivity.class).
//                        putExtra("enter_type","termCar") );

//                ((DetailsCarActivity)getActivity()). setIndexSelecteds(0);
//                ((DetailsCarActivity)getActivity()). radiobuttonComprehensiveSort.setChecked(true);
                MyApp.enter_type="termCar";

                ((DetailsCarActivity)getActivity()).setIndexSelecteds(4);
            }
        });
    }

    @Override
    public void initData() {
        String url = IHttpService.URL2 + IHttpService.GETCHOOSECARLIST;
        OkHttpUtils.post().url(url)
                .addParams("sign", IHttpService.SignVal)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                //提取json
                try {
                    Gson gson = new Gson();
                    selectCarBean = gson.fromJson(response, SelectCarType.class);


                    type_id = selectCarBean.getData().getType().get(0).getType_id();
                    price_id = selectCarBean.getData().getPrice().get(0).getPrice_id();
                    brand_id = selectCarBean.getData().getBrand().get(0).getBrand_id();
                    level_id = selectCarBean.getData().getLevel().get(0).getLevel_id();



                    type_select_tv.setText(selectCarBean.getData().getType().get(0).getName());
                    price_select_tv.setText(selectCarBean.getData().getPrice().get(0).getRange());
                    brand_select_tv.setText(selectCarBean.getData().getBrand().get(0).getName());
                    level_select_tv.setText(selectCarBean.getData().getLevel().get(0).getName());


                    carTypeAdapter = new CarTypeAdapter(getContext(),selectCarBean.getData().getType());
                    typeGridView.setAdapter(carTypeAdapter);
                    typeGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            CarTypeAdapter.POSITION =i;
                            carTypeAdapter.notifyDataSetChanged();
                            type_id = selectCarBean.getData().getType().get(i).getType_id();
                            type_select_tv.setText(selectCarBean.getData().getType().get(i).getName());
                        }
                    });

                    carPriceAdapter = new CarPriceAdapter(getContext(),selectCarBean.getData().getPrice());
                    priceGridView.setAdapter(carPriceAdapter);
                    priceGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            CarPriceAdapter.POSITION =i;
                            carPriceAdapter.notifyDataSetChanged();
                            price_id = selectCarBean.getData().getPrice().get(i).getPrice_id();
                            price_select_tv.setText(selectCarBean.getData().getPrice().get(i).getRange());
                        }
                    });


                    //为品牌增加更多
                    selectCarBean.getData().getBrand().add(new Brand());
                    carBoradAdapter = new CarBoradAdapter(getContext(),selectCarBean.getData().getBrand());
                    boardGridView.setAdapter(carBoradAdapter);
                    boardGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                            if(i==selectCarBean.getData().getBrand().size()-1){
                                startActivityForResult(new Intent(getContext(),ChooseCarModelsActivity.class),10000);
                            }else{
                                brand_id = selectCarBean.getData().getBrand().get(i).getBrand_id();
                                brand_select_tv.setText(selectCarBean.getData().getBrand().get(i).getName());
                            }
                            CarBoradAdapter.POSITION=i;
                            carBoradAdapter.notifyDataSetChanged();
                        }
                    });

                    carLevelAdapter = new CarLevelAdapter(getContext(),selectCarBean.getData().getLevel());
                    levelGridView.setAdapter(carLevelAdapter);
                    levelGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            CarLevelAdapter.POSITION=i;
                            carLevelAdapter.notifyDataSetChanged();
                            level_id = selectCarBean.getData().getLevel().get(i).getLevel_id();
                            level_select_tv.setText(selectCarBean.getData().getLevel().get(i).getName());
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


}
